package com.fourforfo.fourmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.fourforfo.common.exception.BizCodeEnum;
import com.fourforfo.fourmall.product.entity.AttrAttrgroupRelationEntity;
import com.fourforfo.fourmall.product.entity.AttrEntity;
import com.fourforfo.fourmall.product.service.AttrAttrgroupRelationService;
import com.fourforfo.fourmall.product.service.AttrService;
import com.fourforfo.fourmall.product.service.CategoryService;
import com.fourforfo.fourmall.product.vo.AttrGroupRelationVO;
import com.fourforfo.fourmall.product.vo.AttrGroupRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fourforfo.fourmall.product.entity.AttrGroupEntity;
import com.fourforfo.fourmall.product.service.AttrGroupService;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 属性分组
 *
 * @author fourforfo
 * @email fourforfo@gmail.com
 * @date 2022-06-16 12:30:14
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService ;
    //product/attrgroup/{catelogId}/withattr
    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId 接受商品分类id
     */
    @GetMapping("/{catelogId}/withattr")
    public R listGroupAttr(@PathVariable Long catelogId){
        List<AttrGroupRespVO> respVOs = attrGroupService.listGroupAttrByCatId(catelogId);
        return R.ok().put("data", respVOs);
    }

    /**
     * 添加属性分组与属性的关联关系
     * @param relationVOs 接受attrId和attrGroupId
     * @return
     */
    @PostMapping("/attr/relation")
    //product/attrgroup/attr/relation
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVO> relationVOs){

        relationService.saveBatch(relationVOs);
        return R.ok();
    }

    /**
     * 获取属性分组没有关联的其他属性
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R listNoAttrGroup(@RequestParam Map<String, Object> params,@PathVariable Long attrGroupId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrService.queryPageNoAttrGroup(params,attrGroupId);
        return R.ok().put("page", page);
    }

    /**http://localhost:88/api/product/attrgroup/1/noattr/relation?t=1657173534734&page=1&limit=10&key=
     * 删除当前分组关联的属性 [{"attrId":1,"attrGroupId":2}]
     * @param entities 使用AttrAttrgroupRelationEntity封装 attrId 属性id attrGroupId 属性分组id
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R delete(@RequestBody List<AttrAttrgroupRelationEntity> entities){

        if(entities==null||entities.size()==0){
            return R.error(BizCodeEnum.VALID_EXCEPTION.getCode(),BizCodeEnum.VALID_EXCEPTION.getMsg()).put("error","attrId或attrGroupId不能为空");
        }
        relationService.removeByAttrAndGroup(entities);

        return R.ok();
    }

    /**
     *  获取该属性分组下的所有属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R listAttrByGroup(@PathVariable Long attrGroupId){
        if(attrGroupId==null) return R.error(BizCodeEnum.VALID_EXCEPTION.getCode(),BizCodeEnum.VALID_EXCEPTION.getMsg()).put("attrGroupId","不能为空");
        List<AttrEntity> attrs = attrService.listAttrByGroup(attrGroupId);
        return R.ok().put("data", attrs);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
//    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    @Autowired
    CategoryService categoryService;

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
//    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		AttrGroupRespVO respVO = new AttrGroupRespVO();
		BeanUtils.copyProperties(attrGroup,respVO);
		//根据catelogId获取catelogPath
		respVO.setCatelogPath(categoryService.getCatelogPath(respVO.getCatelogId()));

        return R.ok().put("attrGroup", respVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){

        /**
         * 当该分组在attrAttrGroupRelation中没有任何关联的属性信息时
         * 删除该分组信息
         */
		attrGroupService.removeNoAnyRelation(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
